#include					"GameScreen.hpp"
#include					<iostream>

GameScreen::GameScreen(sf::RenderWindow &win)
	: _window(win), _turn(0)
{
	_screenTexture.loadFromFile(GOBAN_PATH);
	_screen.setTexture(_screenTexture);
	_screen.setPosition(GOBAN_ORIGIN_COORD, GOBAN_ORIGIN_COORD);
	buildSquareMap();
}


GameScreen::~GameScreen()
{
}

void						GameScreen::init(char type)
{
	_goban->init(type);
}

void						GameScreen::buildSquareMap()
{	
	_goban = new Goban(_squareMap);
	for (int y = GOBAN_FIRST_BOX_COORD; y <= GOBAN_LAST_BOX_COORD; y += INC)
		for (int x = GOBAN_FIRST_BOX_COORD; x <= GOBAN_LAST_BOX_COORD; x += INC)
			_squareMap.insert(std::pair<int, Square*>(BOX_NUM(x, y), new Square(x, y, _goban->getSquareInfo(BOX_NUM(x, y)), _squareMap)));
}

void						GameScreen::draw()
{
	_window.draw(_screen);
	for (std::map<int, Square*>::iterator it = _squareMap.begin(); it != _squareMap.end(); ++it)
	{
		if ((*it).second->displayable())
			_window.draw((*it).second->getStone());
	}
}

bool						GameScreen::mouseButtonEvent(sf::Event::MouseButtonEvent &event)
{
	if (COORD_VALIDITY(event.x) && COORD_VALIDITY(event.y))
		_goban->putStone(BOX_NUM(event.x, event.y));	
	return (true);
}

int							GameScreen::isThereAWinner()
{
	if (_goban->isThereAWinner())
		return (true);
	return (-1);
}

std::string					GameScreen::getWinner(int winner)
{
	return (_goban->winScreen());
}